package com.cinsea.ext.cjspx;

import com.cinsea.common.Env;
import com.cinsea.common.IDGenerator;
import com.cinsea.common.JdbcDao;
import com.cinsea.htmlselect.model.Option;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.msg.Message;
import com.cinsea.msg.MessageProducer;
import com.cinsea.timer.BaseJob;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;
import org.quartz.JobExecutionContext;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xf
 * Date: 12-9-28
 * Time: 上午11:09
 */
public class MemoJob extends BaseJob {
    private JdbcDao jdbcDao;
    private OptionService optionService;
    private MessageProducer messageProducer;
    private int runtime = 20;//预计job运行一次耗时15'左右

    public MemoJob() {
        jdbcDao = Env.getBean("jdbcDao");
        optionService = Env.getBean("optionService");
        messageProducer = Env.getBean("messageProducer");
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        //remindlist
        String currentDate = DateUtil.getCurrentDate();
        String currentTime = DateUtil.getCurrentTime();
        String currentDatetime = DateUtil.getCurDateTime();
        String remindlistSql = "select processid,ours,others,im,email,message,title,content,recurrrate,startdate,reminddate,remindtime,reminddatemonth,one,two,three,four,five,six,seven from ut_remindmanage where deleted=0 and remindstatus='402881e739f85e350139f8a6aed8003f' and startdate<=?";
        List<Map> remindlist = jdbcDao.queryForList(remindlistSql, new Object[]{currentDate});
        for (Map obj : remindlist) {
            String processid = StringUtil.null2String(obj.get("processid"));
            if (hasLog(processid, currentDate)) continue;
            String ours = StringUtil.null2String(obj.get("ours"));//creator
            String sender = StringUtil.null2String(obj.get("ours"));//creator
            String others = StringUtil.null2String(obj.get("others"));//share
            String pop = String.valueOf(obj.get("im"));//pop msg
            String email = String.valueOf(obj.get("email"));//email msg
            String sms = String.valueOf(obj.get("message"));//sms msg
            String[] msgtype = new String[]{pop, email, sms};
            String title = StringUtil.null2String(obj.get("title"));
            String content = StringUtil.null2String(obj.get("content"));
            String recurrrate = StringUtil.null2String(obj.get("recurrrate"));//frequence specify:4028df2d3a3eb2ba013a3f7684f70309,day:402881e739f85e350139f8a21362001a,week:402881e739f85e350139f8a227cf001b,month:402881e739f85e350139f8a23f16001c,year:402881e739f85e350139f8a25206001d
            String startdate = StringUtil.null2String(obj.get("startdate"));
            String reminddate = StringUtil.null2String(obj.get("reminddate"));
            String remindtime = StringUtil.null2String(obj.get("remindtime"));
            String reminddatemonth = StringUtil.null2String(obj.get("reminddatemonth"));//1-31
            String one = String.valueOf(obj.get("one"));//monday
            String two = String.valueOf(obj.get("two"));
            String three = String.valueOf(obj.get("three"));
            String four = String.valueOf(obj.get("four"));
            String five = String.valueOf(obj.get("five"));
            String six = String.valueOf(obj.get("six"));
            String seven = String.valueOf(obj.get("seven"));
            String[] weekarray = new String[]{seven, one, two, three, four, five, six};
            if (!StringUtil.isEmpty(others)) ours += "," + others;
            if (recurrrate.equals("4028df2d3a3eb2ba013a3f7684f70309")) {//specify day
                if (!StringUtil.isEmpty(remindtime)) {
                    long interval = DateUtil.getMinutesBetween(startdate + " " + remindtime, currentDatetime);
                    if (interval >= 0 && interval < runtime) {//send
                        makeLog(processid, currentDate);
                        sendMsg(processid, title, content, sender, ours, msgtype);
                    }
                }
            }else if (recurrrate.equals("402881e739f85e350139f8a21362001a")) {//each day
                if (!StringUtil.isEmpty(remindtime)) {
                    long interval = DateUtil.getMinutesBetween(currentDate + " " + remindtime, currentDatetime);
                    if (interval >=0 && interval < runtime) {//send
                        makeLog(processid, currentDate);
                        sendMsg(processid, title, content, sender, ours, msgtype);
                    }
                }
            } else if (recurrrate.equals("402881e739f85e350139f8a227cf001b")) {//each week
                int weekday = DateUtil.getDayOfWeek(currentDate);
                if (weekday > -1 && weekarray[weekday].equals("1")) {
                    long interval = DateUtil.getMinutesBetween(currentDate + " " + remindtime, currentDatetime);
                    if (interval >= 0 && interval < runtime) {//send
                        makeLog(processid, currentDate);
                        sendMsg(processid, title, content, sender, ours, msgtype);
                    }
                }


            } else if (recurrrate.equals("402881e739f85e350139f8a23f16001c")) {//each month
                if (!StringUtil.isEmpty(reminddatemonth) && !StringUtil.isEmpty(remindtime)) {
                    Option option = optionService.get(reminddatemonth);
                    if (option != null && option.getObjorder() != null && option.getObjorder() > 0) {
                        String daystr = "" + option.getObjorder();
                        if (option.getObjorder() < 10) daystr = "0" + option.getObjorder();

                        if (daystr.equals(currentDate.substring(currentDate.lastIndexOf("-") + 1))) {
                            long interval = DateUtil.getMinutesBetween(reminddate + " " + remindtime, currentDatetime);
                            if (interval >= 0 && interval < runtime) {//send
                                makeLog(processid, currentDate);
                                sendMsg(processid, title, content, sender, ours, msgtype);
                            }
                        }
                    }
                }

            } else if (recurrrate.equals("402881e739f85e350139f8a25206001d")) {//each year
                if (!StringUtil.isEmpty(reminddate) && !StringUtil.isEmpty(remindtime)) {
                    String currentyear=currentDate.substring(0,4);
                    String month_day=reminddate.substring(4);
                    long interval = DateUtil.getMinutesBetween(currentyear+month_day + " " + remindtime, currentDatetime);
                    if (interval >= 0 && interval < runtime) {//send
                        makeLog(processid, currentDate);
                        sendMsg(processid, title, content, sender, ours, msgtype);
                    }
                }

            }

        }
    }


    private void sendMsg(String processid, String title, String content, String sender, String receivers, String[] type) {//1 pop 2 sms 3 email
        if (type[0].equals("1")) {
            Message msgobj = new Message(Message.MESSAGE_TYPE_POPUP, sender, receivers, title);
            msgobj.setObjId(processid);
            msgobj.setNotifysettingId("928");//备忘
            messageProducer.send(msgobj);
        }

        if (type[1].equals("1")) {
            Message msgobj = new Message(Message.MESSAGE_TYPE_EMAIL, sender, receivers, content, title, null);
            messageProducer.send(msgobj);
        }
        if (type[2].equals("1")) {
            Message msgobj = new Message(Message.MESSAGE_TYPE_SMS, sender, receivers, title);
            messageProducer.send(msgobj);
        }

    }

    private void makeLog(String processid, String date) {
        String id = IDGenerator.getUnquieID();
        String sql = "insert into memolog(id,processid,logdate) values(?,?,?)";
        jdbcDao.update(sql, new Object[]{id, processid, date});
    }

    private boolean hasLog(String processid, String date) {
        boolean haslog = false;
        String sql = "select id from memolog where processid=? and logdate=?";
        List result = jdbcDao.queryForList(sql, new Object[]{processid, date});
        if (result.size() > 0) haslog = true;
        return haslog;
    }
}
